var prefix = ctx + "app/inspecttemplate"
$().ready(function() {
	initLoadColumn();
	loadTable();
	validateRule();
	$("#columnId").chosen("destroy").chosen({
		width : '100%',
		allow_single_deselect : true,
		search_contains : true
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/doEdit",
		data : $('#signupForm').serialize(),
		async : false,
		error : function(request) {
			parent.layer.alert("网络超时");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			templateName : {
				required : true
			}
		},
		messages : {
			templateName : {
				required : icon + "请输入核查项名称"
			}
		},
	})
}
/**
 * 加载表
 */
function loadTable() {
	var html = '';
	$.ajax({
		url : ctx + 'md/business/getTable',
		data : {
			dataType : $('#inspectType').val()
		},
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if(data[i].tableId == $('#otableId').val()){
					html += '<option value="' + data[i].tableId + '" selected>' + data[i].tableComment +  '</option>'
				}else{
					html += '<option value="' + data[i].tableId + '">' + data[i].tableComment +  '</option>'
				}
				
			}
			$("#tableId").append(html);
			//点击事件
			$('#tableId').on('change', function() {
				var a = $('#tableId').val();
				loadColumn(a);
			});
		}
	});
}
/**
 * 加载字段下拉框
 */
function loadColumn(tableId){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/getField',
		data : {
			'tableId' : tableId
		},
		success : function(data) {
			$("#columnId").html("");
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].columnId + '">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'		
			}
			$("#columnId").append(html);
			$("#columnId").chosen("destroy").chosen({
				width : '100%',
				allow_single_deselect : true,
				search_contains : true
			});
		}
	});
}
/**
 * 初始化加载字段下拉框
 */
function initLoadColumn(){
	var html = '';
	$.ajax({
		url : ctx + 'md/business/getField',
		data : {
			'tableId' : $('#otableId').val()
		},
		success : function(data) {
			$("#columnId").html("");
			var columnName = $("#ocolumnName").val();
			var columns = columnName.split(',');
			//加载数据
			for (var i = 0; i < data.length; i++) {
				if(columns.includes(data[i].columnName)){
					html += '<option value="' + data[i].columnId + '" selected>' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'
				}else{
					html += '<option value="' + data[i].columnId + '">' + data[i].columnName +((data[i].columnComment == null || data[i].columnComment == "")?"":" ("+data[i].columnComment+") ")+  '</option>'	
				}
			}
			$("#columnId").append(html);
			$("#columnId").chosen("destroy").chosen({
				width : '100%',
				allow_single_deselect : true,
				search_contains : true
			});
		}
	});
}

